﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject.Business;

namespace eProject.Web.UI.Admin
{
    public partial class CitiesManagementProgram : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateControls();
        }
        private void PopulateControls()
        {
            if (Page.IsPostBack) return;
            BindCitiesSearch();
        }
        private void BindCities()
        {
            try
            {
                grvCities.DataSource = Cities.GetAll();
                grvCities.DataBind();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        private void BindCitiesSearch()
        {
            try
            {
                bool delete = cbDelete.Checked;
                string text = txtSearchText.Text.Trim().ToString();
                grvCities.DataSource = Cities.GetSearch(text,delete);
                grvCities.DataBind();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        protected void grvCities_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvCities.PageIndex = e.NewPageIndex;
            grvCities.DataBind();
            BindCitiesSearch();
        }
        protected void grvCities_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int id = int.Parse(grvCities.DataKeys[e.NewEditIndex].Value.ToString());
            Response.Redirect("CitiesManagementEdit.aspx?Id=" + id);
        }

        protected void grvCities_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = int.Parse(grvCities.DataKeys[e.RowIndex].Value.ToString());
            Cities city = new Cities(id);
            city.IsDeleted = true;
            city.Save();
            BindCitiesSearch();
        }
        protected void grvCities_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = int.Parse(grvCities.DataKeys[e.RowIndex].Value.ToString());
            Cities city = new Cities(id);
            city.IsDeleted = false;
            city.Save();
            BindCitiesSearch();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //bool delete = cbDelete.Checked;
            //string text = txtSearchText.Text.Trim().ToString();
            //grvCities.DataSource = Cities.GetSearch(text,delete);
            //grvCities.DataBind();
            BindCitiesSearch();
        }
    }
}